﻿import flash.display.BitmapData;
import flash.geom.Matrix;
import flash.geom.Rectangle;
import kliment.geom.draw.Draw;
import kliment.display.MCC;
import kliment.geom.draw.DrawStyle;
import kliment.geom.draw.FillBitmapStyle;

/**
 * ...
 * @author	Kliment
 * @version	2.2.1
 */
class kliment.display.MCPattern {
	private var _target_mc:MovieClip;
	private var _mask_mc:MovieClip;
	private var _cell_mc:MovieClip;
	
	private var _min_width:Number = 1;
	private var _min_height:Number = 1;
	private var _max_width:Number = 1;
	private var _max_height:Number = 1;
	
	private var _width:Number = 0;
	private var _height:Number = 0;
	
	private var _onlyIncrease:Boolean = true;
	
	private var _part_identifer:String;
	private var _part_bitmapData:BitmapData;
	
	private var _drawStyle:DrawStyle;
	/**
	 * 
	 * @param	target_mc MovieClip
	 * @param	part String (Identifer name)|BitmapData
	 * @param	options Object {min_width:Number, min_height:Number, width:Number, height:Number, only_increase:Boolean}
	 */
	public function MCPattern(target_mc:MovieClip, part, options:Object) {
		_target_mc = target_mc;
		
		if (!isNaN(options.min_width)) _min_width = options.min_width;
		if (!isNaN(options.min_height)) _min_height = options.min_height;
		if (!isNaN(options.only_increase)) _onlyIncrease = options.only_increase;
		
		if (part.length > 0) {
			_part_identifer = part;
			var temp:MovieClip = MCC.attach(_part_identifer, _target_mc);
			_min_width = temp._width;
			_min_height = temp._height;
			_part_bitmapData = new BitmapData(temp._width, temp._height, true, 0x000000);
			_part_bitmapData.draw(temp);
			MCC.remove(temp);
		} else if (part instanceof BitmapData) {
			_part_bitmapData = part.clone();
			_min_width = _part_bitmapData.width;
			_min_height = _part_bitmapData.height;
		}
		
		_init();
		
		if (!isNaN(options.width)) width = options.width;
		if (!isNaN(options.height)) height = options.height;
	}
	
	private function _init():Void {
		_drawStyle = new DrawStyle(null, null, null, new FillBitmapStyle(_part_bitmapData, null, true, false));
		if (_onlyIncrease) {
			_cell_mc = MCC.create(_target_mc);
			_mask_mc = MCC.create(_target_mc);
			
			Draw.rectangle(_mask_mc, new Rectangle(0, 0, _min_width, _min_height));
			_cell_mc.setMask(_mask_mc);
		} else {
			_cell_mc = _target_mc;
		}
	}
	
	public function get target_mc():MovieClip { return _target_mc }
	
	public function get x():Number { return _target_mc._x }
	public function set x(value:Number):Void { _target_mc._x = value }
	
	public function get y():Number {return _target_mc._y }
	public function set y(value:Number):Void { _target_mc._y = value }
	
	public function get width():Number { return _width }
	public function set width(value:Number):Void {
		setSize(value, _height);
	}
	
	public function get height():Number { return _height }
	public function set height(value:Number):Void {
		setSize(_width, value);
	}
	
	public function setSize(width:Number, height:Number):Void {
		_width = Math.max(width, _min_width);
		_height = Math.max(height, _min_height);
		
		_draw();
	}
	
	private function _draw():Void {
		if (_onlyIncrease) {
			_mask_mc._width = _width;
			_mask_mc._height = _height;
		
			if (_max_width <= _width || _max_height <= _height) {
				_max_width = Math.max(_width, _max_width);
				_max_height = Math.max(_height, _max_height);
				
				_cell_mc.clear();
				Draw.rectangle(_cell_mc, new Rectangle(0, 0, _max_width, _max_height), _drawStyle);
			} 
		} else {
			_cell_mc.clear();
			Draw.rectangle(_cell_mc, new Rectangle(0, 0, _width, _height), _drawStyle);
		}
	}
	
	public function onResize():Void {
		setSize(Stage.width, Stage.height);
	}
	
	public function remove():Void {
		Stage.removeListener(this);
		MCC.remove(_target_mc);
		delete this;
	}
}